package sjsu.android;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import dbAdapter.DailyInfoDBAdapter1;
import dbAdapter.ProfileDBAdapter;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.DatePicker;
import android.widget.Toast;

public class AdvancedExercise extends PreferenceActivity{
	 int year ;
    int month;
    int day;
    DailyInfoDBAdapter1 db;
    int id = 0;
    int caloriesBurnt;
    boolean calVal = false;
    boolean distval = false;
    private ProfileDBAdapter dbp;
    Exercise e;
    static final int DATE_DIALOG_ID = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final Calendar c = Calendar.getInstance();
     
     Bundle bun = getIntent().getExtras();
     month = bun.getInt("monthVal");
     day = bun.getInt("dayVal");
     year = bun.getInt("yearVal");
     System.out.println("Date fields from Advanced " +month +day +year);
//
        try{
		 	dbp = new ProfileDBAdapter(this);
		 	db.open();
		  id = dbp.getId();
		  db.close();
		 }catch (Exception ex){
			 System.out.println("Exception in Custom_ProfileDB");
		 }
        addPreferencesFromResource(R.layout.excercisepreference);
        db = new DailyInfoDBAdapter1(this);
      
        final Preference caloriesBurnedListPref = (Preference) findPreference("caloriesBurnedListPref");
        final Preference distanceListPref = (Preference) findPreference("distanceListPref");
        final Preference durationListPref = (Preference) findPreference("durationListPref");
         caloriesBurnedListPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
         	
 			@Override
 			public boolean onPreferenceChange(Preference arg0, Object arg1) {
 				// TODO Auto-generated method stub
 				distanceListPref.setEnabled(false);
 				calVal = true;
 				distval = false;
 				 
 				durationListPref.setEnabled(false);
 				return true;
 			}
         });
         distanceListPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
         	
 			@Override
 			public boolean onPreferenceChange(Preference arg0, Object arg1) {
 				// TODO Auto-generated method stub
 				caloriesBurnedListPref.setEnabled(false);
 				calVal = false;
 				distval = true;
 				
 				return true;
 			}
         });

        Preference addToProfilePref = (Preference) findPreference("addToProfilePref");
        addToProfilePref
                        .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                                public boolean onPreferenceClick(Preference preference) {
                                	String exerciseName = "Running";
                                    getPrefs(exerciseName);
                                    distanceListPref.setEnabled(true);
                    				durationListPref.setEnabled(true);
                    			caloriesBurnedListPref.setEnabled(true);
                                        SharedPreferences customSharedPreference = getSharedPreferences(
                                                        "myCustomSharedPrefs", Activity.MODE_PRIVATE);
                                        SharedPreferences.Editor editor = customSharedPreference
                                                        .edit();
                                        editor.putString("addToProfilePref",
                                                        "The preference has been clicked");
                                        editor.commit();
                                        return true;
                                }

                        });
        Preference addToProfilePrefWalking = (Preference) findPreference("addToProfilePrefWalking");
        addToProfilePrefWalking
                        .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                        	 public boolean onPreferenceClick(Preference preference) {
                             	String exerciseName = "Walking";
                                 getPrefs(exerciseName);
                                 distanceListPref.setEnabled(true);
                 				durationListPref.setEnabled(true);
                 			caloriesBurnedListPref.setEnabled(true);
                                     SharedPreferences customSharedPreference = getSharedPreferences(
                                                     "myCustomSharedPrefs", Activity.MODE_PRIVATE);
                                     SharedPreferences.Editor editor = customSharedPreference
                                                     .edit();
                                     editor.putString("addToProfilePref",
                                                     "The preference has been clicked");
                                     editor.commit();
                                     return true;
                             }

                     });
        
        Preference addToProfilePrefCycling = (Preference) findPreference("addToProfilePrefCycling");
        addToProfilePrefCycling
                        .setOnPreferenceClickListener(new OnPreferenceClickListener() {

                        	 public boolean onPreferenceClick(Preference preference) {
                             	String exerciseName = "Cycling";
                                 getPrefs(exerciseName);
                                 distanceListPref.setEnabled(true);
                 				durationListPref.setEnabled(true);
                 			caloriesBurnedListPref.setEnabled(true);
                                     SharedPreferences customSharedPreference = getSharedPreferences(
                                                     "myCustomSharedPrefs", Activity.MODE_PRIVATE);
                                     SharedPreferences.Editor editor = customSharedPreference
                                                     .edit();
                                     editor.putString("addToProfilePref",
                                                     "The preference has been clicked");
                                     editor.commit();
                                     return true;
                             }

                     });
       updateCaloriesBurnt();
        
}
    
   
    //the callback received when the user "sets" the date in the dialog
//    private DatePickerDialog.OnDateSetListener mDateSetListener =
//            new DatePickerDialog.OnDateSetListener() {
//
//                public void onDateSet(DatePicker view, int year, 
//                                      int monthOfYear, int dayOfMonth) {
//                    mYear = year;
//                    mMonth = monthOfYear;
//                    mDay = dayOfMonth;
//                }
//            };
//            @Override
//            protected Dialog onCreateDialog(int id) {
//                switch (id) {
//                case DATE_DIALOG_ID:
//                    return new DatePickerDialog(this,
//                                mDateSetListener,
//                                mYear, mMonth, mDay);
//                }
//                return null;
//            }
//            
            
            String caloriesBurnedEditTextPref;
            String distanceEditTextPref;
            String durationEditTextPref;
            String caloriesBurnedListPreference;
            String distanceListPreference;
            String durationListPreference;
            private void getPrefs(String exerciseName) {
            	String calBurnt;
            	caloriesBurnedListPreference = null;
            	distanceListPreference = null;
            	durationListPreference = null;
            	 Bundle bun = getIntent().getExtras();
 			      month = bun.getInt("monthVal");
 			      day = bun.getInt("dayVal");
 			      year = bun.getInt("yearVal");
 			     System.out.println("Date fields from Advanced " +month +day +year);
                    // Get the xml/preferences.xml preferences
                    SharedPreferences prefs = PreferenceManager
                                    .getDefaultSharedPreferences(getBaseContext());
//                   
//                    caloriesBurnedEditTextPref = prefs.getString("caloriesBurnedEditTextPref",
//                                    "Nothing has been entered");
                    caloriesBurnedListPreference = prefs.getString("caloriesBurnedListPref", "nr1");
                    distanceListPreference = prefs.getString("distanceListPref", "Nothing has been entered");
                    durationListPreference = prefs.getString("durationListPref", "nr1");
//                                    distanceEditTextPref = prefs.getString("distanceEditTextPref",
//                                    "Nothing has been entered");
//                                    durationEditTextPref = prefs.getString("durationEditTextPref", "Nothing has been entered");
                    System.out.println("Preferences Selected " +      caloriesBurnedEditTextPref +        distanceEditTextPref +      durationEditTextPref +   day +  month +    year);
                    // Get the custom preference
                    SharedPreferences mySharedPreferences = getSharedPreferences(
                                    "myCustomSharedPrefs", Activity.MODE_PRIVATE);
//                   
                  if (distval) {
                	  int distance = new Integer(distanceListPreference.toString());
                	  int time = new Integer (durationListPreference.toString());
                	  int caloriesBurned = distance * time;
                	  calBurnt = new Integer(caloriesBurned).toString();
                	  insertIntoDBAdvanced(calBurnt, year, month, day, exerciseName);
                  } else{
                    insertIntoDBAdvanced(caloriesBurnedListPreference, year, month, day, exerciseName);
                  }
            }

           public  void insertIntoDBAdvanced(String caloriesBurned, int year, 
                    int monthOfYear, int dayOfMonth, String exerciseName) {
        	   //Lakshmi chk here
        	   String exerciseNameV = exerciseName;
                 String  userIdString = new Integer(id).toString();
                db.open();        
                Date date = convertStringToDate(dayOfMonth, monthOfYear, year);
                String calConsumed = "0";
                String categoryName = "dummy";
                db.insertUserInfo(userIdString,categoryName,exerciseNameV, calConsumed, caloriesBurned, date);
                Cursor cc = db.getAllUsers();
                System.out.println("Inside cursor");
                if (cc.moveToFirst()) {       
                	do {          
                		System.out.println("UserId " +cc.getString(0));
                        System.out.println("Calories Burned " +cc.getString(2));
                        System.out.println("Date"  +cc.getString(3));
                    } while (cc.moveToNext());
                }
                else
                  System.out.println("No data found");
                db.close();
                updateCaloriesBurnt();
                updateSimpleExpandable();
    		 }
            
            public Date convertStringToDate(int date, int month, int year) {
                
                SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                Date theDate = null;
                String in = date + "/" + month + "/" + year;
                try {
                    
                    theDate = dateFormat.parse(in); 
                    return theDate;
                } catch (ParseException e) {
                    e.printStackTrace();
                }
				return theDate;
                
            }
            public void updateCaloriesBurnt(){
                  e = new Exercise();
               	e.updateProgressBar(month,day,year);
               
            }
            public void updateSimpleExpandable(){
            	 e = new Exercise();
            	e.simpleExpandable();
               	e.updateWhatIBurnt();
            }
            
          
}
